#!/bin/bash
file_name=functions.sh
funcs=`which $file_name`
if [ "$funcs" != "" ] ;then
  echo "funcs=$funcs"
  . $funcs
else
  funcs=`dirname $0`/$file_name
  echo "$PWD"
  echo "funcs=$funcs"
  if [ "$funcs" != "" ] ;then
    . $funcs
  else
    exit
  fi
fi

###########################################
CP="cp -rvf"
PARSEINI=`which parseini.shc`
projects_ini=/tmp/projects.ini
result=`get-config.shc http://\`sh_get_svn_hostip\`/repositories/DocsAndTools/scripts/configs/public/projects.ini $projects_ini`
with_app_repo=false
for_release=no
no_update=false
###########################################

function sh_get_project_tag()
{
  local projects_tags=
  local project_config_fs=
  projects_tags=`cat $projects_ini |grep "\[*\]" |tr -d '[|]'|tr '\n' ' '`
  projects_tags=`echo $projects_tags | sed 's/global//g'`
  for f in $projects_tags
  do
  ac_name="`$PARSEINI  "$projects_ini" get "$f" "project_name"`"
  ac_des="`$PARSEINI   "$projects_ini" get "$f" "project_des"`"
  if [ -z "$ac_des" ]; then
    ac_tags="$ac_tags  ${f}_${ac_name}"
  else
    ac_tags="$ac_tags  ${f}_${ac_name}_${ac_des}"
  fi
  done
  ac_tag=`sh_menu_select "$ac_tags"`
  ac_tag=`echo "$ac_tag"|awk -F _ '{print $1}'`
  echo "tag=$ac_tag">&2
  echo $ac_tag
}

function build_qcom()
{
  echoTextBlue "编译qcom代码"
  if [ "$1" = "auto" ]; then
     echo "自动编译qcom项目 ......"
     echo "with_app_repo=$with_app_repo no_update=$no_update"
    if [ "$with_app_repo" = "false" ]; then
      if [ "$no_update" = "false" ]; then
		echo "更新框架代码.............."
		echo "sh_auto_version_qcom $work_copy $product_name $hardware $bbk_version"
        sh_auto_version_qcom $work_copy $product_name $hardware $bbk_version
        if [ "$framework_version" = "" ];then
        	echo "sh_svn_up_frameworks --url $framework_url  $work_copy"
        	sh_svn_up_frameworks --url $framework_url  $work_copy
        else
        	echo "sh_svn_up_frameworks --url $framework_url  $work_copy -r $framework_version"
        	sh_svn_up_frameworks --url $framework_url  $work_copy -r $framework_version
        fi
      fi
    else
      echo "no_update=$no_update"
      if [ "$no_update" = "false" ]; then
		echo "更新框架代码前的预处理，为自动编译更新时不报冲突做准备"
		echo "rm -rf $work_copy/vendor/bbk/common/3Party/"
		rm -rf $work_copy/vendor/bbk/common/3Party/

		echo "开始进行工程的更新操作。。。。。。。。。。。。。。。。。"

		#更新框架代码
		echo "更新框架代码.............."
		echo "sh_auto_version_qcom $work_copy $product_name $hardware $bbk_version"
        sh_auto_version_qcom $work_copy $product_name $hardware $bbk_version

		if [ "$framework_version" = "" ];then
		  echo "sh_svn_up_frameworks --url $framework_url  $work_copy"
		  sh_svn_up_frameworks --url $framework_url  $work_copy
		else
		  echo "sh_svn_up_frameworks --url $framework_url  $work_copy -r $framework_version"
		  sh_svn_up_frameworks --url $framework_url  $work_copy -r $framework_version
		fi
	    #更新应用代码
		echo "更新应用代码.............." 
        rm -rf $work_copy/packages/ 
        if [ "$app_version" = "" ];then
		  echo "svn-update.sh --url  $app_url  $work_copy/packages/  $p_project_name"
		  svn-update.sh --url  $app_url  $work_copy/packages/  $p_project_name
        else
          echo "svn-update.sh --url  $app_url  $work_copy/packages/  $p_project_name -R $app_version"
		  svn-update.sh --url  $app_url  $work_copy/packages/  $p_project_name -R $app_version
        fi
		#更新第三方代码
		if [ "$thirdparty_ver" = "" ];then
			echo "sh_svn_up_thirdparty -p $project_name  -d $product_name --url  $thirdparty_url  --work_copy  $work_copy"
			sh_svn_up_thirdparty -p $project_name  -d $product_name --url  $thirdparty_url  --work_copy  $work_copy
		else
			echo "sh_svn_up_thirdparty -p $project_name  -d $product_name --url  $thirdparty_url  --work_copy  $work_copy -r $thirdparty_ver"
			sh_svn_up_thirdparty -p $project_name  -d $product_name --url  $thirdparty_url  --work_copy  $work_copy -r $thirdparty_ver
		fi
      fi
    fi
	echo "sh_auto_version_qcom $work_copy $product_name $hardware $bbk_version"
	sh_auto_version_qcom $work_copy $product_name $hardware $bbk_version

    #设置环境变量，开始编译
    cd $work_copy
    if [ -z "$hardware" ]; then
      echo "auto build must give hardware version"
      exit
    fi
    echo "开始设置环境变量......"
    echo ". ./setenv.sh -p $project_name -h $hardware -d $product_name -bv $build_variant"
	. ./setenv.sh -p $project_name -h $hardware -d $product_name -bv $build_variant
	
    if test -f $work_copy/packages/.version && test -f $work_copy/.version; then
       echo " ========== 当前编译的版本信息 ========== "
       echo " ========== 框架: `cat $work_copy/.version`"
       echo " ========== 应用: `cat $work_copy/packages/.version`"
    fi
    echo "make $build"
    make clean
    make $build
    cd -
    
  else
  
    echoTextBlue  "开始手动编译编译qcom代码 ......"
    if test -f $work_copy/packages/.version; then
      min_no_app=`cat $work_copy/packages/.version`   
    else
      min_no_app=`sh_get_svn_head_no_from_url $app_url`
    fi
    if [ -z "$min_no_app" ]; then
      min_no_app=0
    fi
    max_no_app=`sh_get_svn_head_no_from_url $app_url`

    if test -f $work_copy/.version; then
      min_no_framework=`cat $work_copy/.version`
      max_no_framework=`sh_get_svn_head_no_from_url $framework_url`
    else
      min_no_framework=`sh_get_svn_committed_no_from_copy $work_copy`
      max_no_framework=`sh_get_svn_head_no_from_url $framework_url`
    fi

    if [ "$with_app_repo" = "false" ]; then
      result=`question "是否下载整个主线代码,未包含应用仓库？" 1 no` 
      if [  "$result" = "yes" ] ; then
        echoTextBlue "开始更新整个主线代码,未包含应用仓库 ......"
        #svn.shc up $work_copy
      fi
    else
      result=`question "是否下载框架代码？" 1 no`
      if [  "$result" = "yes" ] ; then
        result=`question "请输入版本号: [$min_no_framework ~ $max_no_framework]" 2 "$max_no_framework"`
        sh_svn_up_frameworks --url $framework_url $work_copy  -r $result
        echo $result > $work_copy/.version
      fi
      #更新应用代码
      result=`question "是否下载应用代码 ？" 1 no`
      if [  "$result" = "yes" ] ; then
        result=`question "请输入版本号: [$min_no_app ~ $max_no_app]" 2 "$max_no_app"`
        svn-update.sh --url  $app_url  $work_copy/packages/  -p $project_name  -r $result
      fi
      #更新mode
      result=`question "是否从modem编译服务器更新modem ？" 1 no`
      if [  "$result" = "yes" ] ; then
        svn-update.sh --url  $modem_url  $work_copy/mediatek/custom/common/modem/  -p $project_name
      fi
      #更新第三方apk
      result=`question "是否从thirdparty更新第三方apk？" 1 no`
      if [  "$result" = "yes" ] ; then
        svn_update_thirdparty $product_name
      fi
    fi
     #设置环境变量，开始编译
    cd $work_copy
    if [ -z "$hardware" ]; then
      echo "auto build must give hardware version"
      exit
    fi
    echo "开始设置环境变量......"
    echo ". ./setenv.sh -p $project_name -d $product_name -bv $build_variant"

    if test -f $work_copy/packages/.version && test -f $work_copy/.version; then
       echo " ========== 当前编译的版本信息 ========== "
       echo " ========== 框架: `cat $work_copy/.version`"
       echo " ========== 应用: `cat $work_copy/packages/.version`"
    fi
    echo "make $build"
    make $build
    cd -
  fi
}

function build_mtk()
{
  if [ "$1" = "test" ]; then  
    cd $work_copy
    if [ "$no_update" != "true" ]; then
      echo "sh_incremental_svn_up_all $work_copy $product_name $framework_version $app_version"
      sh_incremental_svn_up_all $work_copy $product_name $framework_version $app_version
    fi
    echo "sh_auto_version_mtk $work_copy $product_name $bbk_version"
    sh_auto_version_mtk $work_copy $product_name $bbk_version
    echo "开始设置环境变量......"
    echo ". ./setenv.sh $hardware $for_release"
    . ./setenv.sh $hardware $for_release 
    if test -f $work_copy/out/target/product/$product_name/system.img; then
      rm $work_copy/out/target/product/$product_name/system.img -rf
    fi 
    echo "./makeMtk $product_name javaoptgen"
    ./makeMtk $product_name javaoptgen 
    echo "./makeMtk $product_name emigen nandgen ptgen drvgen"
    ./makeMtk $product_name emigen nandgen ptgen drvgen
    echo "./makeMtk $options $product_name $build"
    ./makeMtk $options $product_name $build
    cd -
    return
  fi
  if [ "$1" = "auto" ]; then
     echo "自动编译mtk项目 ......"
     echo "with_app_repo=$with_app_repo no_update=$no_update"
    if [ "$with_app_repo" = "false" ]; then
      if [ "$no_update" = "false" ]; then
	echo "rm -rf $work_copy/mediatek/*/packages/Bluetooth/"
	test -d $work_copy/mediatek/source/packages/Bluetooth/ && rm -rf $work_copy/mediatek/source/packages/Bluetooth/ 
	test -d $work_copy/mediatek/packages/apps/Bluetooth/ && rm -rf $work_copy/mediatek/packages/apps/Bluetooth/ 
	echo "更新框架代码.............."
	echo "sh_auto_version_mtk $work_copy $product_name $bbk_version"
        sh_auto_version_mtk $work_copy $product_name $bbk_version
        if [ "$framework_version" = "" ];then
        	echo "sh_svn_up_frameworks --url $framework_url  $work_copy"
        	sh_svn_up_frameworks --url $framework_url  $work_copy
        else
        	echo "sh_svn_up_frameworks --url $framework_url  $work_copy -r $framework_version"
        	sh_svn_up_frameworks --url $framework_url  $work_copy -r $framework_version
        fi
      fi
    else
      echo "no_update=$no_update"
      if [ "$no_update" = "false" ]; then
	echo "更新框架代码前的预处理，为自动编译更新时不报冲突做准备"
	echo "rm -rf $work_copy/vendor/bbk/common/3Party/"
	rm -rf $work_copy/vendor/bbk/common/3Party/
	echo "rm -rf $work_copy/mediatek/source/packages/*/Bluetooth/"
	test -d $work_copy/mediatek/source/packages/Bluetooth/ && rm -rf $work_copy/mediatek/source/packages/Bluetooth/ 
	test -d $work_copy/mediatek/packages/apps/Bluetooth/ && rm -rf $work_copy/mediatek/packages/apps/Bluetooth/ 
	echo "rm -rf $work_copy/vendor/mediatek/${product_name}/artifacts/out/target/product/${product_name}/system/app/"
	rm -rf $work_copy/vendor/mediatek/${product_name}/artifacts/out/target/product/${product_name}/system/app/
	echo "rm -rf $work_copy/mediatek/config/${product_name}/"
	rm -rf $work_copy/mediatek/config/${product_name}/
	echo "开始进行工程的更新操作。。。。。。。。。。。。。。。。。"

        #更新框架代码
	echo "更新框架代码.............."
	echo "sh_auto_version_mtk $work_copy $product_name $bbk_version"
        sh_auto_version_mtk $work_copy $product_name $bbk_version

	if [ "$framework_version" = "" ];then
	  echo "sh_svn_up_frameworks --url $framework_url  $work_copy"
	  sh_svn_up_frameworks --url $framework_url  $work_copy
	else
      echo "sh_svn_up_frameworks --url $framework_url  $work_copy -r $framework_version"
	  sh_svn_up_frameworks --url $framework_url  $work_copy -r $framework_version
	fi
	#更新应用代码
	echo "更新应用代码.............." 
	rm -rf $work_copy/packages/ 
	if [ "$app_version" = "" ];then
	  if [ "$temp_app_project_name" = "" ];then
		echo "svn-update.sh --url  $app_url  $work_copy/packages/  $p_project_name"
		svn-update.sh --url  $app_url  $work_copy/packages/  $p_project_name
	  else
		echo "svn-update.sh --url  $app_url  $work_copy/packages/  $temp_app_project_name"
		svn-update.sh --url  $app_url  $work_copy/packages/  $temp_app_project_name
	  fi
	else
	  if [ "$temp_app_project_name" = "" ];then
	    echo "svn-update.sh --url  $app_url  $work_copy/packages/  $p_project_name -R $app_version"
		svn-update.sh --url  $app_url  $work_copy/packages/  $p_project_name -R $app_version
	  else
	    echo "svn-update.sh --url  $app_url  $work_copy/packages/  $temp_app_project_name -R $app_version"
		svn-update.sh --url  $app_url  $work_copy/packages/  $temp_app_project_name -R $app_version
	  fi
	fi
	#更新modem
	#echo "svn-update.sh -m modem  -t $product_name -p $project_name -h $hardware --work_copy  $work_copy"
	#svn-update.sh -m modem  -t $product_name -p $project_name -h $hardware --work_copy  $work_copy
        #更新第三方代码
	#svn-update.sh -m apk -p $project_name --work_copy $work_copy -t $product_name
	if [ "$thirdparty_ver" = "" ];then
		echo "sh_svn_up_thirdparty -p $project_name  -d $product_name --url  $thirdparty_url  --work_copy  $work_copy"
		sh_svn_up_thirdparty -p $project_name  -d $product_name --url  $thirdparty_url  --work_copy  $work_copy
	else
		echo "sh_svn_up_thirdparty -p $project_name  -d $product_name --url  $thirdparty_url  --work_copy  $work_copy -r $thirdparty_ver"
		sh_svn_up_thirdparty -p $project_name  -d $product_name --url  $thirdparty_url  --work_copy  $work_copy -r $thirdparty_ver
	fi
	
      fi
    fi
    echo "sh_auto_version_mtk $work_copy $product_name $bbk_version"
    sh_auto_version_mtk $work_copy $product_name $bbk_version
    #设置环境变量，开始编译
    cd $work_copy
    if [ -z "$hardware" ]; then
      echo "auto build must give hardware version"
      exit
    fi
    echo "开始设置环境变量......"
    echo ". ./setenv.sh $hardware $for_release"
    . ./setenv.sh $hardware $for_release

    if [ "$build_for" = "sdk" ]; then
      echoTextBlue "build for $build_for"
    elif [ "$build_for" = "emulator" ]; then
      echoTextBlue "build: ./makeMtk $options emulator"
      ./makeMtk $options emulator
    elif [ "$build_for" = "banyan_addon" ]; then
      echoTextBlue "build: ./makeMtk $options banyan_addon"
      ./makeMtk $options banyan_addon
    else
      if [ "$build" != "new" ]; then
        echo "./makeMtk $product_name javaoptgen"
        ./makeMtk $product_name javaoptgen 
        echo "./makeMtk $product_name emigen nandgen ptgen drvgen"
        ./makeMtk $product_name emigen nandgen ptgen drvgen
      fi
      echo "./makeMtk $options $product_name $build"
      ./makeMtk $options $product_name $build
      ./make-boot.sh root
    fi

    if test -f $work_copy/packages/.version && test -f $work_copy/.version; then
       echo " ========== 当前编译的版本信息 ========== "
       echo " ========== 框架: `cat $work_copy/.version`"
       echo " ========== 应用: `cat $work_copy/packages/.version`"
    fi
    cd -
  else
    echoTextBlue  "开始手动编译mtk项目 ......"
    if test -f $work_copy/packages/.version; then
      min_no_app=`cat $work_copy/packages/.version`   
    else
      min_no_app=`sh_get_svn_head_no_from_url $app_url`
    fi
    if [ -z "$min_no_app" ]; then
      min_no_app=0
    fi
    max_no_app=`sh_get_svn_head_no_from_url $app_url`

    if test -f $work_copy/.version; then
      min_no_framework=`cat $work_copy/.version`
      max_no_framework=`sh_get_svn_head_no_from_url $framework_url`
    else
      min_no_framework=`sh_get_svn_committed_no_from_copy $work_copy`
      max_no_framework=`sh_get_svn_head_no_from_url $framework_url`
    fi

    if [ "$with_app_repo" = "false" ]; then
      result=`question "是否下载整个主线代码,未包含应用仓库？" 1 no` 
      if [  "$result" = "yes" ] ; then
        echoTextBlue "开始更新整个主线代码,未包含应用仓库 ......"
        #svn.shc up $work_copy
      fi
    else
      result=`question "是否下载框架代码？" 1 no`
      if [  "$result" = "yes" ] ; then
        result=`question "请输入版本号: [$min_no_framework ~ $max_no_framework]" 2 "$max_no_framework"`
        sh_svn_up_frameworks --url $framework_url $work_copy  -r $result
        echo $result > $work_copy/.version
      fi
      #更新应用代码
      result=`question "是否下载应用代码 ？" 1 no`
      if [  "$result" = "yes" ] ; then
        result=`question "请输入版本号: [$min_no_app ~ $max_no_app]" 2 "$max_no_app"`
        svn-update.sh --url  $app_url  $work_copy/packages/  -p $project_name  -r $result
      fi
      #更新mode
      result=`question "是否从modem编译服务器更新modem ？" 1 no`
      if [  "$result" = "yes" ] ; then
        svn-update.sh --url  $modem_url  $work_copy/mediatek/custom/common/modem/  -p $project_name
      fi
      #更新第三方apk
      result=`question "是否从thirdparty更新第三方apk？" 1 no`
      if [  "$result" = "yes" ] ; then
        svn_update_thirdparty $product_name
      fi
    fi
    #设置环境变量，开始编译
    cd $work_copy
    echoTextBlue "开始设置环境变量......"
    if [ -n "$hardware" ]; then
      . ./setenv.sh $hardware $for_release
    else
      ./setenv.sh
    fi
    ##
    if [ "$build_for" = "sdk" ]; then
      echoTextBlue "build for $build_for"
    elif [ "$build_for" = "emulator" ]; then
      echoTextBlue "build for $build_for"
    elif [ "$build_for" = "banyan_addon" ]; then
      echoTextBlue "build for $build_for"
    else
      if [ "$build" != "new" ]; then
        echo "./makeMtk $product_name javaoptgen"
        ./makeMtk $product_name javaoptgen 
        echo "./makeMtk $product_name emigen nandgen ptgen drvgen"
        ./makeMtk $product_name emigen nandgen ptgen drvgen
      fi
      echo "./makeMtk $options $product_name $build"
      ./makeMtk $options $product_name $build
    fi
    ##
    cd -
  fi
}

#脚本入口
while test $# != 0
do
    case $1 in
	--username)
	    shift
	    username=$1
	    ;; 
	--password)
	    shift
	    password=$1
	    ;; 
	-m)
	    shift
	    mode=$1
	    ;; 
	-a)
	    shift
	    auto_version=$1
	    ;; 
	-v)
	    shift
	    bbk_version=$1
	    ;; 
	-t)
	    shift
	    type=$1
	    ;; 
    -o)
	    shift
	    options=$1
	    ;;
    -b)
	    shift
	    build=$1
	    ;;
    -bv)
	    shift
	    build_variant=$1
	    ;;
    --build-for)
	    shift
	    build_for=$1
	    ;;
    -h)
	    shift
	    hardware=$1
	    ;;
    -p)
	    shift
	    project_name=$1
	    ;;
    -d)
	    shift
	    product_name=$1
	    ;;
    --work-copy | --work_copy)
	    shift
	    work_copy=$1
	    ;;
	--temp-app)
	    shift
	    temp_app_project_name=$1
	    ;;
    --framework-url | --framework_url)
	    shift
	    framework_url=$1
	    ;;
    -R)
	    shift
	    framework_version=$1
	    ;;
    --app_url | --app-url)
	    shift
	    app_url=$1
	    ;;
    --thirdparty_url | --thirdparty-url)
 	    shift
	    thirdparty_url=$1
	    ;;

    -r)
	    shift
	    app_version=$1
	    ;;
    -tv)
	    shift
	    thirdparty_ver=$1
	    ;;
    --modem_url | --modem-url)
		shift
		modem_url=$1
		;;
    --with_app_repo)
	    shift
	    with_app_repo=true
	    for_release=yes
	    ;;
    --with-app-repo)
	    with_app_repo=true
	    for_release=yes
	    ;;
    --no-update)
	    no_update=true
	    ;;
    *)
	   work_copy=$1
	   ;;
       esac
       shift
done

if [ -z "$mode" ]; then
  mode=mannul
fi
if [ -z "$build" ]; then
  build=remake
fi

#判断工作拷贝路径
if [ -z "$work_copy" ]; then
  work_copy=`pwd`
fi
work_copy=`echo $work_copy |sed "s@~@$HOME@g"`

echo "
     work_copy = $work_copy
framework_url = $framework_url
      app_url = $app_url"


#获取源代码仓库路径


if [ -z "$project_name" ] || [ -z "$product_name" ]; then
  #获取项目信息
  project_tag=`sh_get_project_tag`
  if [ -z "$project_name" ]; then #TD1108
  project_name="`$PARSEINI  "$projects_ini" get "$project_tag" "project_name"`"
  fi
  if [ -z "$project_type" ]; then #mtk
  project_type="`$PARSEINI  "$projects_ini" get "$project_tag" "project_type"`"
  fi
  if [ -z "$product_name" ]; then #bbk75_emmc_gb2
  product_name="`$PARSEINI  "$projects_ini" get "$project_tag" "product_name"`"
  fi
  if [ -z "$app_url" ]; then
      app_url="`$PARSEINI  "$projects_ini" get "$project_tag" "app_url"`"
  fi
fi
#
  if [ -z "$framework_url" ]; then
  framework_url=`sh_get_svn_url $work_copy `
  fi
  if [ -z "$app_url" ]; then
      app_url=`sh_get_svn_url $work_copy/packages/apps/Phone |grep apps/Phone/Phone`
      if [ -n "$app_url" ]; then
	ac_replace=`basename $app_url`
        app_url=`sh_get_svn_url $work_copy/packages/apps/Phone |sed "s#/apps/Phone/$ac_replace##g"` 
      fi
  fi
#
if [ -z "$framework_url" ] || [ -z "$app_url" ]; then
  if [ "$with_app_repo" = "true" ]; then
    echo "未指定相应仓库路径,程序退出!!!"
    exit
  fi
fi
if [ -n "$temp_app_project_name" ]; then
	temp_app_project_name="-p $temp_app_project_name"
fi

if [ -n "$project_name" ]; then
  p_project_name="-p $project_name"
else
  echoTextRed "下载应用代码必须指定项目名称"
  exit
fi

#获取项目类型
echo "获取项目类型... $work_copy/makeMtk"
if [ -z "$project_type" ]; then
  if test -f $work_copy/makeMtk; then
    project_type=mtk
  else
    project_type=qcom
  fi
fi

#打印配置信息
echo "
         mode = $mode
        build = $build
 project_name = $project_name
     hardware = $hardware
 project_type = $project_type
     work_copy = $work_copy
framework_url = $framework_url
 product_name = $product_name
      app_url = $app_url"



#开始编译项目
echo "build_$project_type $mode"
build_$project_type $mode
exit

#mtk命令示例：
automake.sh --framework_url http://svn/repositories/MT6573/trunk/alps-w1140-20111007 --app_url http://svn/repositories/AppsOfMtk/trunk/packages-w1140/  --work_copy ~/workspace/gphone/MT6573/PD1110/alps-w1140-20111007 -p PD1110  -d bbk73_gb -h PD1110MB0 -b new -m auto
#高通命令示例：
automake.sh --framework_url http://svn/repositories/MT6573/trunk/alps-w1140-20111007 --app_url http://svn/repositories/AppsOfMtk/trunk/packages-w1140/  --work_copy ~/workspace/gphone/MT6573/PD1110/alps-w1140-20111007 -p PD1110  -d bbk73_gb -h PD1110MB0


